"""
1.读取文件；
2.去除所有标点符号和换行符，并把所有大写变成小写；
3.并相同的词，统计每个词出现的频率，并按照词频从大到小排序；
4.将结果按行输出到文件 out.txt。
"""
import re


def parse(text):
    text = re.sub(r'[^\w ]', ' ', text)
    text = text.lower()
    word_list = text.split(' ')
    word_list = filter(None, word_list)
    word_cnt = {}
    for word in word_list:
        if word not in word_cnt:
            word_cnt[word] = 0
        word_cnt[word] += 1
    sorted_word_cnt = sorted(word_cnt.items(), key=lambda kv: kv[1], reverse=True)
    return sorted_word_cnt


with open('D:\\file\\in.txt', 'r') as fin:
    text = fin.read()

word_and_freq = parse(text)

with open('D:\\file\\out.txt', 'w') as fout:
    for word, freq in word_and_freq:
        fout.write('{} {}\n'.format(word, freq))
